package getools4me;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.BlogCommentFile;
import domain.BlogFile;
import domain.MultiPart;

public class ReceiveServlet extends MyHttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3109545382005245654L;

	HttpServletRequest request;
	HttpServletResponse response;
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.request = req;
		this.response = resp;
		
		// get multipart
		MultiPart mp = new MultiPart(req.getInputStream());
		
		
		// get method name
		String mdName = mp.getValue("method");
		if(mdName==null) {
			error("null method name");
			return;
		}
		
		// get the method
		Method md = null;
		try {
			md = this.getClass().getDeclaredMethod(mdName, HttpServletRequest.class, HttpServletResponse.class,MultiPart.class);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		if(md==null) {
			error("not found method",mdName);
			return;
		}
		
		// invoke the method
		try {
			md.invoke(this, req,resp,mp);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * actually get value from names: title content 
	 * @param mp
	 */
	public void recevieBlog(HttpServletRequest req, HttpServletResponse resp,MultiPart mp)
	throws ServletException, IOException {
		
		// get title
		byte[] title = mp.getData("title");
		if(title==null || title.length==0){
			error("title can not be empty!");
			return;
		}

		// get content
		
		if(mp.getData("content")==null || mp.getData("content").length==0){
			error("content can not be empty!");
			return;
		}
		// new 
		BlogFile file = new BlogFile();
		file.setTitle(new String(title));
		file.setContent(mp.getData("content"));
		file.setAuthor(mp.getValue("author"));
		file.setEmail(mp.getValue("email"));
		
		// persistent
		BlogManager.saveBlog(file);
		
		// redirect to blog list page
		String posturl = mp.getValue("posturl");
		if(posturl!=null){
			resp.sendRedirect(posturl);
		}else{ 
			resp.sendRedirect("/blog/home.jsp");
		}
		
	}
	
	
	/**
	 * save blog draft, was called by client ajax, the Category is BlogDraft
	 * @param req
	 * @param resp
	 * @param mp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void recevieBlogDraft(HttpServletRequest req, HttpServletResponse resp,MultiPart mp)
	throws ServletException, IOException {
		
	
	}
	
	public void recevieBlogComment(HttpServletRequest req, HttpServletResponse resp,MultiPart mp)
	throws ServletException, IOException {
		// check author,email,blogID,content
		byte[] author = mp.getData("author");
		if(author==null || author.length==0){
			error("name can not be empty!");
			return;
		}
		byte[] email = mp.getData("email");
		if(email==null || email.length==0){
			error("email can not be empty!");
			return;
		}
		String blogID = mp.getValue("blogID");
		if(blogID==null || blogID.length()==0){
			error("blogID can not be empty!");
			return;
		}
		if(mp.getData("content")==null || mp.getData("content").length==0){
			error("content can not be empty!");
			return;
		}
		
		
		
		BlogFile file_org = BlogManager.getBlogFileById(Long.valueOf(blogID));
		if(file_org==null){
			error("Blog with this id:blogID can not be found!");
			return;
		}
		
		
		// new comment file
		BlogCommentFile file = new BlogCommentFile();
		file.setAuthor(new String(author));
		file.setEmail(new String(email));
		file.setTitle(mp.getValue("title"));
		file.setContent(mp.getData("content"));
		file.setBlog_ID(Long.valueOf(blogID));
		
		// persistent
		Long id_comment =  BlogManager.saveComment(file);
		
		// add comment file to original file
		if(id_comment==null){
			error("Commnet id can not be retrieved!");
			return;
		}
		
		BlogManager.addComment(Long.valueOf(blogID), id_comment);
		
		
		// redirect to blog list page
		String posturl = mp.getValue("posturl");
		if(posturl!=null){
			resp.sendRedirect(posturl+"?blogID="+blogID+"#blogCommentID_"+id_comment);
		}else{ 
			resp.sendRedirect("/blog/home.jsp");
		}
	}
	
	
	public void recevieFile(HttpServletRequest req, HttpServletResponse resp,MultiPart mp)
	throws ServletException, IOException {
		
	}
	
	public void manifest(HttpServletRequest req, HttpServletResponse resp,MultiPart mp)
	throws ServletException, IOException {
		System.out.println(mp.manifest());
	}
	
	public void error(String... msgs){
		if(this.response==null){
			for(String msg : msgs){
				System.err.println(msg);
			}
		}else{
			for(String msg : msgs){
				try {
					response.getWriter().write(msg);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.sendError(404,"You should not access this page in 'get' way!     ");
	}
	
}
